const app = getApp()
import BaseModel from '../../utils/common.js'

var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//早会id
    userOrder:"",//表单数据
    teacherValueId:"",//选择的讲师id
    teacherValueName:"选择讲师",//选择的讲师名字
    earlyTime: "必填项",//早会时间
    /* dateTimeArray: null,*/
    /*dateTime: null,*/
    dateTimeArray1: null, //全部数组时间日期
    dateTime1: null, //当前选中时间日期
    startYear: 2000, //开始年份
    endYear: 2050 //结束年份
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    this.setData({
      id:options.id,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
    this.getEarlyOrderData(); //获取早会信息
  },
  /*日期*/
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    var earlyTime= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    console.log(earlyTime)
    this.setData({
      earlyTime: earlyTime
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length-1];
    if(currPage.data.staffIdAll){ //如果选中的id有值，就把原来的覆盖掉
      this.setData({
        teacherValueId:currPage.data.staffIdAll,
        teacherValueName:currPage.data.staffNameAll,
      })
    }
  },
  /*提交表单信息*/
  formSubmit: function(e) {
    if(e.detail.value.contacts==""){
      wx.showToast({
        title: '联系人未填写',
        icon: 'none'
      })
      return false;
    }else if(!(/^1[3456789]\d{9}$/.test(e.detail.value.phone))){
      wx.showToast({
        title: '电话格式不正确',
        icon: 'none'
      })
      return false;
    } else if (this.data.earlyTime == "必填项") {
      wx.showToast({
        title: '时间未选择',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.company==""){
      wx.showToast({
        title: '公司未填写',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.address==""){
      wx.showToast({
        title: '地址未填写',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.estimated_number==""){
      wx.showToast({
        title: '人数未填写',
        icon: 'none'
      })
      return false;
    }else if(!(/^\d+$/.test(e.detail.value.estimated_number))){
      wx.showToast({
        title: '人数只能填写数字',
        icon: 'none'
      })
      return false;
    }else if(this.data.teacherValueId==undefined){
      wx.showToast({
        title: '讲师未指定',
        icon: 'none'
      })
      return false;
    }
    var tutor_id = this.data.teacherValueId.toString();
    e.detail.value["time"] = this.data.earlyTime;
    e.detail.value["tutor_id"] = tutor_id;
    this.setData({
      userOrder: e.detail.value
    })
    if(this.data.id){
      this.getEditUserOrderData();//修改
    }else{
      this.getUserOrderData();//提交
    }
  },
  //获取早会信息
  getEarlyOrderModel:new BaseModel({
    url: "detailsMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEarlyOrderData(){
    this.getEarlyOrderModel
        .fetch({
          morningId:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            earlyMes:res.data,
            teacherValueName:res.data.tutor_name,
            teacherValueId:res.data.tutor_id,
            earlyTime: res.data.time
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  //提交信息
  getUserOrderModel:new BaseModel({
    url: "addMorning",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getUserOrderData(){
    this.getUserOrderModel
        .fetch({
          morning:this.data.userOrder
        })
        .then(res => {
          //成功
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          //失败 2018-12-20 00:00:00
        })
  },
  //修改提交信息
  getEditUserOrderModel:new BaseModel({
    url: "totalUpdate",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEditUserOrderData(){
    this.getEditUserOrderModel
        .fetch({
          morningId:this.data.id,
          morning:this.data.userOrder,
        })
        .then(res => {
          //成功
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          //失败 2018-12-20 00:00:00
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  /*onShareAppMessage: function () {

  }*/
})